/********************************************************************
 * Filename:	BitOps.c
 * Description:	s
 *				Also holds functions to manipulate metatable entries
 * 
 * Group 2:		nachOS
 * Author(s):	Jan Durakiewicz
 * 
 * Class:		CSE 325
 * Instructor:	David Duggan
 * Assignment:	Lab #6 Page Replacement
 * Assigned:	4/2/2013
 * Due:			4/9/2013
 ********************************************************************/
#include "BitOps.h"
unsigned long maxAddress = 16383; //14 1's


short unsigned int get_use_table_at(short unsigned int index){
    int segment = index / (8 * sizeof(long));
	int bit_number = index % (8 * sizeof(long));
	return (useTable[segment] & ((unsigned long)1 << bit_number)) >> bit_number;
}

void set_use_table_at(short unsigned int index, short unsigned int value){
    int segment = index / (8 * sizeof(long));
	int bit_number = index % (8 * sizeof(long));

	if(value == 1)
		useTable[segment] |= ((unsigned long)1) << bit_number;
	else
		useTable[segment] &= ~((unsigned long)1 << bit_number);
}

short unsigned int get_page_validity(unsigned short int* entry){
	if(*entry > maxAddress)	//in binary, this means that there's a bit after the 14th bit
		return 1;
	else
		return 0;
}

short unsigned int get_page_address(short unsigned int* entry){
	if(*entry > maxAddress)						//if the entry is valid, it's value will be over max
		return (*entry) - (maxAddress + 1);		//get rid of the valid bit
	else
		return *entry;
}

void set_page_address(short unsigned int* entry, short unsigned int address){
	if(*entry > maxAddress)
		*entry = (maxAddress + 1) + address;
	else
		*entry = address;
}

void set_page_validity(short unsigned int* entry, short unsigned int isValid){
	if(isValid == 1)
		*entry = *entry | (maxAddress + 1);
	else
		*entry = *entry & maxAddress; //maxAddress == invalidMask
}





